﻿-- Author      : Johannes
-- Create Date : 1/17/2008 7:04:07 PM

-- START UI FUNCTIONS --

__SBDKP_TYPE_ALPHA = 0
__SBDKP_TYPE_NUMERIC = 1
__SBDKP_TYPE_NUMERIC_REVERSE = 2

function SB_DKP:UISortList(toSort, type) -- sorts the inputted table, according to type. Check the top of the file for definitions. The default sorting methodology is that of quicksort. I may implement alternatives.
	if (SB_DKP_DEBUG >= 1) then
	SB_DKP:Print("UISortList: called")
	end
	if (toSort == nil) then
		SB_DKP:PrintError("UISortList: No data passed!")
		return 1
	end
	if (#(toSort) == 1) then
		-- seriously, let's just return the tables with one entry.
		if (SB_DKP_DEBUG >= 1) then
			SB_DKP:Print("UISortList: Single-entried table passed... passing it back")
			SB_DKP:Print("UISortList: Returning ("..toSort[1][1]..","..toSort[1][2]..").")
		end
		return toSort
	end
	if (#(toSort[1]) < 2) then
		SB_DKP:PrintError("UISortList: Unusable table passed!")
	end
	
	-- At this point, assume safe execution.
	
	local tempTable	-- for the resulting sorted items.
	
	if (type == nil or type == __SBDKP_TYPE_NUMERIC) then
		-- I'm attempting to use an in-library sorting function. Should work...
		table.sort(toSort, function(a,b) return a[2]>b[2] end)
		if (SB_DKP_DEBUG > 0) then
			SB_DKP:Print("UiSortList: Table sorted")
		end
	-- I discovered the reverse sort by accident... anyway, it's here now :-)
	elseif (type == __SBDKP_TYPE_NUMERIC_REVERSE) then
		table.sort(toSort, function(a,b) return a[2]<b[2] end)
		if (SB_DKP_DEBUG > 0) then
			SB_DKP:Print("UiSortList: Table sorted")
		end	
	elseif (type == __SBDKP_TYPE_ALPHA) then
		-- Sort by name, alphabetically
		
	else
		-- Otherwise error report it.
		SB_DKP:PrintError("UiSortList: Unusable sort type passed")
	end
end

-- This function generates usable HTML for the old simpleHTML method of displaying DKP. This specific function was for the name half of the display.
-- DEPRECATED. This function is no longer supported and probably broken already now.
function SB_DKP:UIGenerateHTMLNames(FoundCharData) -- returns well-formatted HTML to put into the left column of the SB_DKP frame.
	if (FoundCharData == nil or #(FoundCharData) == 0) then
		if (SB_DKP_DEBUG >= 1) then
			self:Print("UIGenerateHTMLNames: Could not generate HTML - the table is invalid")
		end
		return nil
	end
	local returnHTML = ""
	local tempString = ""
	local HTMLstart = "<p align=\"left\">"
	local HTMLend = "</p>"
	
	for index,chardata in ipairs(FoundCharData) do
		tempString = HTMLstart..chardata[1]..HTMLend
		if (SB_DKP_DEBUG > 1) then
			self:Print("UIGenerateHTMLNames: Constructed string \"" .. tempString .. "\".")
		end
		returnHTML = returnHTML..tempString
		if (SB_DKP_DEBUG > 1) then
			self:Print("UIGenerateHTMLNames: Added " .. chardata[1] .. "to the HTML list (returnHTML).")
		end
	end
	returnHTML = "<html><body>" .. returnHTML .. "</body></html>"
	return returnHTML
end

-- This function generates usable HTML for the old simpleHTML method of displaying DKP. This specific function was for the DKP half of the display.
-- DEPRECATED. This function is no longer supported and probably broken already now.
function SB_DKP:UIGenerateHTMLDKP(FoundCharData) -- returns well-formatted HTML to put into the right column of the SB_DKP frame.
	if (FoundCharData == nil or #(FoundCharData) == 0) then
		if (SB_DKP_DEBUG >= 1) then
			self:Print("UIGenerateHTMLDKP: Could not generate HTML - the table is invalid")
		end
		return nil
	end
	local returnHTML = ""
	local tempString = ""
	local HTMLstart = "<p align=\"left\">"
	local HTMLend = "</p>"
	for index,chardata in ipairs(FoundCharData) do
		tempString = HTMLstart..chardata[2]..HTMLend
		if (SB_DKP_DEBUG > 1) then
			self:Print("UIGenerateHTMLDKP: Constructed string \"" .. tempString .. "\".")
		end
		returnHTML = returnHTML..tempString
		if (SB_DKP_DEBUG > 1) then
			self:Print("UIGenerateHTMLDKP: Added " .. chardata[2] .. "to the HTML list (returnHTML).")
		end
	end
	returnHTML = "<html><body>" .. returnHTML .. "</body></html>"
	return returnHTML
end

function SB_DKP:UISearch() -- function uses contents of EditBox to search.
	if (SB_DKP_SearchBox:GetText() == "") then -- avoid too much work should the search box be empty.
		return nil
	end
	if (SB_DKP_DEBUG > 1) then
		SB_DKP:Print("UISearch: Starting search for '" .. SB_DKP_SearchBox:GetText() .. "'.")
	end
	SB_DKP.UI.Current = SB_DKP:GetChar(SB_DKP_SearchBox:GetText(),"chardata")

	-- Do the sort
	self:UISortList(SB_DKP.UI.Current, nil)
	-- End the sort

	SB_DKP_ScrollFrame_Update() -- kick in the updated information in the window.
end

-- DEPRECATED. The old function to list current party/raid DKP. It's replaced with the one below.
function SB_DKP:UIListGroupOLD()
SB_DKP:RaidDBUpdate()
	local numMembers = GetNumRaidMembers()
	if (SB_DKP_DEBUG >= 1) then
		self:Print("UIListGroup: GetNumRaidMembers() returned "..numMembers..".")
	end
	if (numMembers == 0) then -- if 0, we're not in a raid. Ergo, do party check.
		if (SB_DKP_DEBUG >= 1) then
			self:Print("UIListGroup: Party list mode.")
		end
		numMembers = GetNumPartyMembers()
		local partydata = {}
		local memberindex = {}
		for iterator=1,numMembers do
			memberindex = self:GetChar(UnitName("party"..iterator), "index_exact")
			if (memdata ~= nil) then -- a hit!
				tinsert(partydata, {UnitName("party"..iterator), SB_DKP_DATA[memberindex]})
				if (SB_DKP_DEBUG > 1) then
					self:Print("UIListGroup: Added "..partydata[1].." ("..partydata[2]..").")
				end
			end
		end
	elseif (numMembers > 0) then -- here, we presume raid.
		if (SB_DKP_DEBUG >= 1) then
			self:Print("UIListGroup: Raid list mode.")
		end
		local raidindices = self:RaidGetDKPTier()
		if (SB_DKP_DEBUG >= 1) then
			self:Print("UIListGroup: Retrieved raid index table. Length: " .. #(raidindices) .. ".")
		end
		
		local nameHTML = self:UIGenerateHTMLNames(raidindices)
		local DKPHTML = self:UIGenerateHTMLDKP(raidindices)
		SB_DKP_Results_Names:SetText(nameHTML)
		SB_DKP_Results_DKP:SetText(DKPHTML)
	else
		self:PrintError("UIListGroup: Error! GetNumRaidMembers() returned weird number!")
	end
end

function SB_DKP:UIListGroup()
SB_DKP:RaidDBUpdate()
	local numMembers = GetNumRaidMembers()
	if (SB_DKP_DEBUG >= 1) then
		self:Print("UIListGroup: GetNumRaidMembers() returned "..numMembers..".")
	end
	if (numMembers == 0) then -- if 0, we're not in a raid. Ergo, do party check.
		if (SB_DKP_DEBUG >= 1) then
			self:Print("UIListGroup: Party list mode.")
		end
		numMembers = GetNumPartyMembers()
		local partydata = {}
		local memberindex = {}
		for iterator=1,numMembers do
			memberindex = self:GetChar(UnitName("party"..iterator), "index_exact")
			if (memdata ~= nil) then -- a hit!
				tinsert(partydata, {UnitName("party"..iterator), SB_DKP_DATA[memberindex]})
				if (SB_DKP_DEBUG > 1) then
					self:Print("UIListGroup: Added "..partydata[1].." ("..partydata[2]..").")
				end
			end
		end
	elseif (numMembers > 0) then -- here, we presume raid.
		if (SB_DKP_DEBUG >= 1) then
			self:Print("UIListGroup: Raid list mode.")
		end
		local raidindices = self:RaidGetDKPTier()
		if (SB_DKP_DEBUG >= 1) then
			self:Print("UIListGroup: Retrieved raid index table. Length: " .. #(raidindices) .. ".")
		end
		SB_DKP.UI.Current = SB_DKP:RaidGetDKPTier(curtier)
		SB_DKP:UISortList(SB_DKP.UI.Current, nil)
		SB_DKP_ScrollFrame_Update()
	else
		self:PrintError("UIListGroup: Error! GetNumRaidMembers() returned weird number!")
	end
end

function SB_DKP:UIResizeDefault()
	SB_DKP_TempFrame:SetHeight(256)
	SB_DKP_TempFrame:SetWidth(256)
end

function SB_DKP:UITier45() -- displays DKP numbers for raid members in the same T4/5 group as your toon.
	-- failsafe. We won't be using this in groups... ever.
	if (GetNumRaidMembers() == 0) then
		self:PrintError("Not printing Tiers 4/5... not in a group.")
		return nil
	end
	
	local playerclass, unused, mode
	unused, playerclass = strlower(UnitClass(UnitName("player")))
	if (playerclass == "druid" or playerclass == "warrior" or playerclass == "priest") then
		mode = SB_DKP.Tier.Defender
	elseif  (playerclass == "shaman" or playerclass == "rogue" or playerclass == "paladin") then
		mode = SB_DKP.Tier.Champion	
	elseif  (playerclass == "hunter" or playerclass == "mage" or playerclass == "warlock") then
		mode = SB_DKP.Tier.Hero
	end	
	
	if (SB_DKP_DEBUG >= 1) then
		self:Print("Player class " .. playerclass .. " detected. Mode " .. mode .. " set.")
	end
	self:UISortList(raidindices, nil)	
	self:UITierTBCLowSearch(mode)
end
	-- UGLIEST FUNCTION NAME EVER --
	function SB_DKP:UITierTBCLowSearch(group) -- searches the raid for members of the particular Tier 4/5 set.
		local numMembers = GetNumRaidMembers()
		if (SB_DKP_DEBUG >= 1) then
			self:Print("UITierTBCLowSearch: GetNumRaidMembers() returned " .. numMembers .. ".")
		end
		if (numMembers == 0) then -- if 0, we're not in a raid. Ergo, do party check.
			if (SB_DKP_DEBUG >= 1) then
				self:Print("UITierTBCLowSearch: Party list mode.")
			end
			numMembers = GetNumPartyMembers()
			local partydata = {}
			local memberindex = {}
			for iterator=1,numMembers do
				memberindex = self:GetChar(UnitName("party" .. iterator), "index_exact")
				if (memdata ~= nil) then -- a hit!
					if (SB_DKP_DATA[memberindex] == nil) then -- pug safety.
						tinsert (partydata, {UnitName("party" .. iterator), 0})
					else
						tinsert(partydata, {UnitName("party" .. iterator), SB_DKP_DATA[memberindex]})
					end
					if (SB_DKP_DEBUG > 1) then
						self:Print("UITierTBCLowSearch: Added " .. partydata[1] .. " (" .. partydata[2] .. ").")
					end
				end
			end
		elseif (numMembers > 0) then -- here, we presume raid.
			if (SB_DKP_DEBUG >= 1) then
				self:Print("Raid list mode.")
			end
			local raidindices = self:RaidGetDKPTier()
			if (SB_DKP_DEBUG >= 1) then
				self:Print("UITierTBCLowSearch: Retrieved raid index table. Length: " .. #(raidindices) .. ".")
			end
			self:UISortList(raidindices, nil)
			local nameHTML = self:UIGenerateHTMLNames(raidindices)
			local DKPHTML = self:UIGenerateHTMLDKP(raidindices)
			SB_DKP_Results_Names:SetText(nameHTML)
			SB_DKP_Results_DKP:SetText(DKPHTML)
		else
			self:PrintError("UITierTBCLowSearch: Error! GetNumRaidMembers() returned weird number!")
		end
	end

function SB_DKP:UITier6() -- displays DKP numbers for raid members in the same T6 group as your toon.

end

	--- SUB: START DROPDOWN MENU FUNCTIONS ---
	
	function SB_DKP.DropDown.TierTBCLo()
		SB_DKP:RaidDBUpdate()
		if (SB_DKP_DEBUG >= 1) then
			SB_DKP:Print("DropDown.TierTBCLo: Finding this toon's lower-tier comparisons.")
		end
		local curtier, curclass
		curclass = strlower(UnitClass(UnitName("player")))
		if (curclass == "druid" or curclass == "priest" or curclass == "warrior") then
			curtier = SB_DKP.Tier.Defender
		elseif (curclass == "shaman" or curclass == "rogue" or curclass == "paladin") then
			curtier = SB_DKP.Tier.Champion
		elseif (curclass == "mage" or curclass == "hunter" or curclass == "warlock") then
			curtier = SB_DKP.Tier.Hero
		else
			SB_DKP:PrintError("DropDown.TierTBCLo: You have an invalid class!")
			return nil
		end
		SB_DKP.UI.Current = SB_DKP:RaidGetDKPTier(curtier)
		SB_DKP:UISortList(SB_DKP.UI.Current, nil)
		SB_DKP_ScrollFrame_Update()
	end
	
	-- Updated to work with the GUI 2.0. Slight cleanups have also been made.
	function SB_DKP.DropDown.TierTBCHi() -- EXACTLY the same as above, only with T6 class tiers.
		SB_DKP:RaidDBUpdate()
		if (SB_DKP_DEBUG >= 1) then
			SB_DKP:Print("DropDown.TierTBCHi: Finding this toon's hier-tier comparisons.")
		end
		local curtier, curclass
		curclass = strlower(UnitClass(UnitName("player")))
		if (curclass == "druid" or curclass == "mage" or curclass == "rogue") then
			curtier = SB_DKP.Tier.Vanquisher
		elseif (curclass == "shaman" or curclass == "hunter" or curclass == "warrior") then
			curtier = SB_DKP.Tier.Protector
		elseif (curclass == "warlock" or curclass == "priest" or curclass == "paladin") then
			curtier = SB_DKP.Tier.Conqueror
		else
			SB_DKP:PrintError("DropDown.TierTBCHi: You have an invalid class!")
			return nil
		end
		SB_DKP.UI.Current = SB_DKP:RaidGetDKPTier(curtier)
		SB_DKP:UISortList(SB_DKP.UI.Current, nil)
		SB_DKP_ScrollFrame_Update()
	end

	function SB_DKP.DropDown.Druid()
		SB_DKP.DropDown.PrintClass("druid")
	end

	function SB_DKP.DropDown.Rogue()
		SB_DKP.DropDown.PrintClass("rogue")
	end

	function SB_DKP.DropDown.Warrior()
		SB_DKP.DropDown.PrintClass("warrior")
	end

	function SB_DKP.DropDown.Warlock()
		SB_DKP.DropDown.PrintClass("warlock")
	end

	function SB_DKP.DropDown.Mage()
		SB_DKP.DropDown.PrintClass("mage")
	end

	function SB_DKP.DropDown.Shaman()
		SB_DKP.DropDown.PrintClass("shaman")
	end

	function SB_DKP.DropDown.Priest()
		SB_DKP.DropDown.PrintClass("priest")
	end

	function SB_DKP.DropDown.Paladin()
		SB_DKP.DropDown.PrintClass("paladin")
	end

	function SB_DKP.DropDown.Hunter()
		SB_DKP.DropDown.PrintClass("hunter")
	end

	-- Function to print out a certain class within the raid. Updated to GUI 2.0 (FauxScrollFrame).
	function SB_DKP.DropDown.PrintClass(class)
		SB_DKP:RaidDBUpdate()
		if (SB_DKP_DEBUG >= 1) then
			SB_DKP:Print("Finding all " .. class .. "s within raid.")
		end
		SB_DKP.UI.Current = SB_DKP:RaidGetDKPClass(class)
		SB_DKP:UISortList(SB_DKP.UI.Current, nil)
		SB_DKP_ScrollFrame_Update()
	end

	--- SUB: STOP DROPDOWN MENU FUNCTIONS ---

-- END UI FUNCTIONS --